<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CategorySmallRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [];
        switch (request()->method) {
            case 'POST':
                $rules['name'] = 'required|max:100|unique:category_small,name';
                break;
            case 'PUT':
                $rules['name'] = 'required|max:100|unique:category_small,name,' . request()->category_small;
                break;
            default:
                break;
        }

        return $rules;
    }

    public function messages()
    {
        return [
            'name.required' => '请输入分类名称！',
            'name.max' => '分类名称长度最大为100个字符！',
            'name.unique' => '分类名称已经存在！',
        ];
    }
}
